#pragma once

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class InetAddr
{
private:
    void GetAddress(std::string *ip, uint16_t *port)
    {
        //将端口号从网络字节序转换为主机字节序
        *port = ntohs(m_addr.sin_port);
        //将IP地址从网络字节序转换为点分十进制字符串
        *ip = inet_ntoa(m_addr.sin_addr);
    }

public:
    InetAddr(const struct sockaddr_in &addr)
        : m_addr(addr)
    {
        GetAddress(&m_ip, &m_port);
    }

    std::string GetIp()
    {
        return m_ip;
    }

    uint16_t GetPort()
    {
        return m_port;
    }

    bool operator==(const InetAddr &addr)
    {
       if(m_ip==addr.m_ip && m_port==addr.m_port)
       {
        return true;
       }

       return false;
    }

     struct sockaddr_in GetAddr()
    {
        return m_addr;
    }

    ~InetAddr()
    {
    }

private:
    struct sockaddr_in m_addr; // 地址结构体
    std::string m_ip;          // ip
    uint16_t m_port;           // 端口
};